#include "StdAfx.h"
#include "List.h"


bool List::AddHead(int data)
{
	NODE *new_ele = new NODE;
	new_ele->CreateNODE(data);
	if (!new_ele) return 0;
	if (pHead == NULL)
	{
		pHead = new_ele;
	}
	else
	{
		new_ele->pNext = pHead;
		pHead = new_ele;
	}
	return 1;
}
void List::PrintList()
{
	NODE* pNODE = pHead;
	while(pNODE!= NULL)
	{
		printf("%5d", pNODE->data );
		pNODE = pNODE->pNext;
	}
}
void List::RemoveAll()
{
	NODE* pNODE;
	while(pHead!=NULL)
	{
		pNODE = pHead;
		pHead = pHead->pNext;
		delete pNODE;
	}
	pHead = NULL;
}
int List::SumList()
{
	int sum = 0;
	NODE* pNODE = pHead;
	while( pNODE!= NULL)
	{
		sum += pNODE->data;
		pNODE = pNODE->pNext;	
	}
	return sum;

}
int List::MaxList()
{
	NODE* pNODE = pHead;
	int max = pHead->data;
	while( pNODE!= NULL)
	{
		if (max < pNODE->data)
			max = pNODE->data;
		pNODE = pNODE->pNext;	
	}
	return max;
}
int List::CountList()
{
	int count = 0;
	NODE* pNODE = pHead;
	while( pNODE!= NULL)
	{
		count++;
		pNODE = pNODE->pNext;	
	}
	return count;
}
int List::CountData( int data)
{
	int count = 0;
	NODE* pNODE = pHead;
	while( pNODE!= NULL)
	{
		if(pNODE->data == data) 
			count++;
		pNODE = pNODE->pNext;	
	}
	return count;
}
bool List::AddTail( int data)
{
	NODE* new_ele = new NODE;
	new_ele->CreateNODE(data);
	if (!new_ele) {return 0;}
	NODE* pNODE = pHead;
	if (pHead == NULL)
	{
		pHead = new_ele;
	}
	else
	{
		while(pNODE->pNext!= NULL)
		{
			pNODE = pNODE->pNext;
		}
		pNODE->pNext = new_ele;
	}
	return 1;
}
bool List::RemoveHead()
{
	if ( pHead == NULL)
		return 0;

	NODE* pNODE = pHead;
	pHead = pHead->pNext;
	delete pNODE;
	return 1;
}
bool List::RemoveTail()
{
	
	NODE* pNODE = pHead;

	if (pHead == NULL )
		return 0;

	if(pHead->pNext == NULL)
	{
		delete pNODE;
		pHead = NULL;
		return 1;
	}
	
	while(pNODE->pNext->pNext!= NULL)
	{
		pNODE = pNODE->pNext;
	}
	delete pNODE->pNext;
	pNODE->pNext = NULL;
	return 1;

}
NODE* List::GetNode(int pos)
{
	int i;
	NODE *pNode = pHead;
	for ( i = 0 ; i < pos ; i++)
	{
		pNode = pNode->pNext;
	}
	return pNode;
}


void List::SeperateLL(List &pHead1, List &pHead2, int x)
{
	NODE* pNODE = pHead;

	if ( pHead == NULL)
		return;
	else
	{
		while ( pNODE!=NULL )
		{
			if ( pNODE->data < x )
				pHead1.AddTail(pNODE->data);
			else
				if ( pNODE->data > x )
					pHead2.AddTail(pNODE->data);
			pNODE = pNODE ->pNext;
		}
	}

}
